let express = require('express');
let crypto = require('crypto');
let router = express.Router();
let filter = require('../../components/filter');

/**
 * 查询指标数据 by p 2018-06-11
 *
 * 按条件查询指标数据
 *
 */
router.get('/', filter.authorize, function (req, res, next) {
    let params = req.query;

    var limits = "";
    var sql = "";
    var datas = [];

    req.app.db.query("select count(1) as totalCount from quota " + sql, datas, function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results[0].totalCount === 0) {
            res.json({"success": true, "data": {}, "totalCount": 0});
            return;
        }

        let totalCount = results[0].totalCount;

        if (params.pageNo > 0 && params.pageSize > 0) {
            limits = " limit ?, ?";
            datas.push((params.pageNo - 1) * params.pageSize);
            datas.push(params.pageSize * 1);
        }

        req.app.db.query("select * from quota " + limits, datas, function (err, results, fields) {
            if (err) {
                res.json({"success": false, "message": global.databaseErr, "detail": err});
                return;
            }

            res.json({"success": true, "data": results, "totalCount": totalCount, "message": ""});
        });
    });
});

/**
 * 查看指标数据 by p 2018-06-11
 *
 * 按id查看指定的指标信息
 */
router.get('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query("select * from quota where id = ?", [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results.length === 0) {
            res.json({"success": false, "message": global.noDataErr});
            return;
        }

        res.json({"success": true, "data": results[0]});
    });
});

/**
 * 修改指标数据 by p 2018-06-11
 *
 *
 */
router.put('/:id', filter.authorize, function (req, res, next) {
    let params = req.body;
    let isStatistics = 0;
    let distributeDepartmentId = "0101";

    if (params.id === undefined || params.id === "" || params.id === 0) {
        res.json({"success": false, "message": "指标编码不能为空！"});
        return;
    }

    if (params.year === undefined || params.year === "") {
        res.json({"success": false, "message": "指标年度不能为空！"});
        return;
    }

    if (params.departmentId === undefined || params.departmentId === "") {
        res.json({"success": false, "message": "指标下发单位编码不能为空！"});
        return;
    }

    if (params.isStatistics > 0) {
        isStatistics = params.isStatistics;
    }

    if (!(params.num >= 0)) {
        res.json({"success": false, "message": "指标数必须大于等于0！"});
        return;
    }

    req.app.db.query("call proc_quota_distribute(?, ?, ?, ?, ?, ?)", [params.id, params.year, params.departmentId, isStatistics, params.num, distributeDepartmentId], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        let result = results[0][0]._result;

        res.json({"success": result === 1, "message": global.quotaMsg.distribute[result]});
    });
});

/**
 * 新增指标数据 by p 2018-06-11
 *
 *
 */
router.post('/', filter.authorize, function (req, res, next) {
    let params = req.body;
    let isStatistics = 0;
    let distributeDepartmentId = "0101";

    if (params.year === undefined || params.year === "") {
        res.json({"success": false, "message": "指标年度不能为空！"});
        return;
    }

    if (params.departmentId === undefined || params.departmentId === "") {
        res.json({"success": false, "message": "指标下发单位编码不能为空！"});
        return;
    }

    if (params.isStatistics > 0) {
        isStatistics = params.isStatistics;
    }

    if (!(params.num >= 0)) {
        res.json({"success": false, "message": "指标数必须大于等于0！"});
        return;
    }

    req.app.db.query("call proc_quota_distribute(?, ?, ?, ?, ?, ?)", [0, params.year, params.departmentId, isStatistics, params.num, distributeDepartmentId], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        let result = results[0][0]._result;

        res.json({"success": result === 1, "message": global.quotaMsg.distribute[result]});
    });
});

/**
 * 删除指标信息 by v 2018-06-11
 *
 * 按id删除指定的指标信息
 */
router.post('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;
    let distributeDepartmentId = "0101";

    req.app.db.query("call proc_quota_delete(?, ?)", [params.id, distributeDepartmentId], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        let result = results[0][0]._result;

        res.json({"success": result === 1, "message": global.quotaMsg.delete[result]});
    });
});

module.exports = router;